﻿using UnityEngine;
using System.Collections;

public class PlayerWhite : Player 
{
	private GameObject[] whiteOnly;
	private GameObject[] blackOnly;
	
	private string nameHolding;
	
	void Start () 
	{
		whiteOnly = GameObject.FindGameObjectsWithTag("OnlyWhite");
		blackOnly = GameObject.FindGameObjectsWithTag("OnlyBlack");
	}

	protected new void Update()
	{
		base.Update();
		if (!base.blackActive)
		{
			whiteGuy.GetComponent<Collider2D>().collider2D.enabled = true;
			blackGuy.GetComponent<Collider2D>().collider2D.enabled = false;
			whiteGuy.GetComponent<SpriteRenderer>().sortingOrder = 1;
			blackGuy.GetComponent<SpriteRenderer>().sortingOrder = 0;

			base.Movement ("Horizontal", "Vertical");
			if (transform.childCount > 0)
			{
				if (base.lastPosition == 0)
				{
					transform.FindChild(nameHolding).transform.position = new Vector3 (transform.position.x, transform.position.y-0.4f, 0f);
				}
				else if (base.lastPosition == 1)
				{
					transform.FindChild(nameHolding).transform.position = new Vector3 (transform.position.x-0.4f, transform.position.y, 0f);
				}
				else if (base.lastPosition == 2)
				{
					transform.FindChild(nameHolding).transform.position = new Vector3 (transform.position.x+0.4f, transform.position.y, 0f);
				}
				else if (base.lastPosition == 3)
				{
					transform.FindChild(nameHolding).transform.position = new Vector3 (transform.position.x, transform.position.y+0.4f, 0f);
				}
			}
		}
		else
		{
			base.animator.SetInteger("Vertical", 0);
			base.animator.SetInteger("Horizontal", 0);
		}

		if (Input.GetButtonUp("Interaction")) 
		{
			transform.DetachChildren();
		}
	}
	
	void OnCollisionStay2D(Collision2D other)
	{
//		if (other.transform.tag == "White")
//		{
//			if (Input.GetButtonDown("Interaction")) 
//			{
//				transform.DetachChildren();
//				other.transform.parent = transform;
//				nameHolding = other.transform.name;
//			}
//		}
		if (!blackActive)
		{
			if (Input.GetButton("Interaction")) //Fire1 = q
			{
				if (other.transform.tag == "Fim")
				{

					if(jogadorPerto)
					{
						Application.LoadLevel(5);
					}
					else
					{
						if (blackActive)
						{
							blackGuy.gameObject.GetComponent<PlayerBlack>().blackActive = false;
							whiteGuy.gameObject.GetComponent<PlayerWhite>().blackActive = false;
						}
						else
						{
							blackGuy.gameObject.GetComponent<PlayerBlack>().blackActive = true;
							whiteGuy.gameObject.GetComponent<PlayerWhite>().blackActive = true;
						}
					}
				}
			}
		}
	}
}
